var com = {};
com.c_id = '#comment_box';
com.b_url = 'http://www.kekenet.com/bianji/newcms/admin/index.php/';
com.l = '<dl class="say_box_dl"><dt><img id="icon" src="http://uc.kekenet.com/avatar.php?uid=0&size=small" width="48" height="48" /></dt><dd><div><a><strong><font id="username"></font>:</strong></a><font id="creat_at"></font></div><span id="content"></span><p><a href="javascript:void(0)" class="link_gree">&nbsp;</a>　<a href="javascript: void(0);" id="support_btn" class="link_gree">顶一下</a>(<span id="support">0</span>)</p><div id="reply_0" class="huifu_send" style="display:none"></div></dd></dl>';
com.l_frm = '';

com.d = function(frm){
	var id = $(com.c_id).attr('data-id');
	$.post(com.b_url + 'login.post_comment', {
		id: id,
		content: $(frm).find('textarea[name=content]').val()
	}, function(){
		$(frm).find('textarea[name=content]').val(''); // clear
		com.load(id, 1);
	}, 'json');
	return false;
};
com.load = function(id, pg){
	if($(com.c_id).html() == '') {
		$(com.c_id).html('loading...');
	}
	$.getJSON(com.b_url + 'login.load_comment', { id: id, pg : pg }, function(d){
		if($(com.c_id).html() == 'loading...') {
			$(com.c_id).html(com.l_frm);
		}
		$(com.c_id).find('#cnt').html(d.cnt);
		$(com.c_id).find('#list').html('');
		$(d.datas).each(function(k, v){
			var h = $(com.l);
			h.find('#creat_at').html(v.creat_at);
			h.find('#username').html(v.username);
			h.find('#content').html(v.content);
			h.find('#support').html(v.support);
			h.find('#support_btn').one('click', function(){ // support click
				$.getJSON(com.b_url + 'login.support_comment', { id: v.id }, function(d){
					if(d.s == 1)
						h.find('#support').html(++v.support);
				});
			});
			if(v.userid != '0') {
				h.find('#icon').attr('src', 'http://uc.kekenet.com/avatar.php?uid=' + v.userid + '&size=small');
			}
			$(com.c_id).find('#list').append(h);
		});
		
		var html = '';
		for(var i = 1; i <= Math.ceil(d.cnt / 3); i++) {
			if(pg==i){
				html += '<li><a class="page_number" href="javascript: com.load($(com.c_id).attr(\'data-id\'), ' + i + ')">' + i + '</a></li>';
			}else{
				html += '<li><a href="javascript: com.load($(com.c_id).attr(\'data-id\'), ' + i + ')">' + i + '</a></li>';
			}
			//html += '<li><a href="javascript: com.load($(com.c_id).attr(\'data-id\'), ' + i + ')">' + i + '</a></li>';
		}
		$(com.c_id).find('#page').html(html);
	});
};


$(document.body).ready(function(){
	com.load($(com.c_id).attr('data-id'), 1);
});
